package self.zhanpeng.http.proxy;

import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ProxyServletConfiguration implements EnvironmentAware {
    @Override
    public void setEnvironment(Environment environment) {

    }

    @Value("${proxy.uri}")
    String targetUri;

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean servletRegistrationBean
                = new ServletRegistrationBean(new ProxyServlet(), "/proxy/*");
        Map<String, String> params = new HashMap<>(4);
        params.put("targetUri",targetUri);
        params.put("log", "true");
        servletRegistrationBean.setInitParameters(params);
        return servletRegistrationBean;
    }
}
